package database.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import advice.ExceptionManager;
import database.pojo.UserRechargeInfo;
import manager.pay.type.PayResultInfo;

/**
 * 类名:UserRechargeDao
 * 描述:对用户充值表的数据库封装
 * @author zk
 * @date 2018-4-20
 * @version 0.0.1
 *
 */
public class UserRechargeDao{
	private static Logger logger = Logger.getLogger(ExceptionManager.class);
	public boolean insert(UserRechargeInfo uInfo, Connection conn ) throws SQLException {
		String sqlInsert = "insert into user_recharge(recharge_type, mount, recharge_orderid,mid,recharge_status,recharge_time,recharge_description,uid,recharge_day,packetId) values( ? , ? , ?, ?, ?,	?,	?,	? ,?,?)";
		PreparedStatement pstmt = null;
		boolean result = true;
		try{
			pstmt = conn.prepareStatement(sqlInsert);//相当于python操作数据库的游标
			pstmt.setInt(1, uInfo.getRecharge_type());
			pstmt.setDouble(2, uInfo.getMount());
			pstmt.setString(3, uInfo.getRecharge_orderid());
			pstmt.setInt(4, uInfo.getMid());
			pstmt.setInt(5, uInfo.getRecharge_status());
			pstmt.setString(6, uInfo.getRecharge_time());
			pstmt.setString(7, uInfo.getRecharge_description());
			pstmt.setInt(8, uInfo.getUid());
			pstmt.setString(9, uInfo.getRecharge_day());
			pstmt.setInt(10, uInfo.getPacketId());
			pstmt.executeUpdate();
		}catch (Exception e) {
			//System.out.println(" UserConsumeDao insert Exception" + e);
			logger.info(e);
			result = true;
		}
		finally{
			if(pstmt != null){
				pstmt.close();
			}
		}
		return result;
		
	}

	public boolean isExitOrderSnAndPaySucc(String out_trade_no, Connection conn) throws SQLException {
		String sqlSelect = "select recharge_orderid from user_recharge where recharge_orderid = ? and recharge_status = ?";
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try{
			pstmt = conn.prepareStatement(sqlSelect);
			pstmt.setString(1, out_trade_no);
			pstmt.setInt(2, 1);
			rs = pstmt.executeQuery();
			if(rs.next())

				if(rs.getString("recharge_orderid") != null) {
					//System.out.println("isExitOrderSnAndPaySucc:"+rs.getString("recharge_orderid"));
					logger.info("isExitOrderSnAndPaySucc:"+rs.getString("recharge_orderid"));
					return true;
				}
				else {
					return false;
				}
			else {
				 return false;
			}
		}
		finally{
			if(rs != null){
				rs.close();
			}
			if(pstmt != null){
				pstmt.close();
			}
		}
	}

	public PayResultInfo getPayResultInfo(String outTradeNo, Connection conn) throws SQLException {
		String sqlSelect = "select mid,mount,packetId,recharge_description from user_recharge where recharge_orderid=?";
		PayResultInfo req = new PayResultInfo();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try{
			pstmt = conn.prepareStatement(sqlSelect);
			pstmt.setString(1, outTradeNo);
			rs = pstmt.executeQuery();
			if(rs.next()) {
				req.setMachineId(rs.getInt("mid"));
				req.setOut_trade_no(outTradeNo);
				req.setPrice(rs.getInt("mount"));
				req.setTradeid(rs.getInt("packetId"));
				req.setRecharge_description(rs.getString("recharge_description"));
			}
		}
		finally{
			if(rs != null){
				rs.close();
			}
			if(pstmt != null){
				pstmt.close();
			}
		}
		return req;
	}

	public boolean updateUserRecharge(UserRechargeInfo uInfo, Connection conn) throws SQLException {
		String sqlUpdate = "update user_recharge set recharge_time = ? , recharge_day = ? , recharge_status = ? where recharge_orderid = ? ";
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try{
			pstmt = conn.prepareStatement(sqlUpdate);
			pstmt.setString(1, uInfo.getRecharge_time());
			pstmt.setString(2, uInfo.getRecharge_day());
			pstmt.setInt(3, uInfo.getRecharge_status());
			pstmt.setString(4, uInfo.getRecharge_orderid());
			pstmt.executeUpdate();
			
			
		}catch (Exception e) {
			System.out.println(" updateUserRecharge Exception" + e);
		}
		finally{
			if(rs != null){
				rs.close();
			}
			if(pstmt != null){
				pstmt.close();
			}
		}
		return true;
	}

	public int selectUid(String out_trade_no, Connection conn) throws SQLException {
		String sqlSelect = "select uid from user_recharge where recharge_orderid=? limit 1";
		int uid=0;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try{
			pstmt = conn.prepareStatement(sqlSelect);
			pstmt.setString(1, out_trade_no);
			rs = pstmt.executeQuery();
			if(rs.next()) {
				uid=rs.getInt("uid");
			}
		}
		finally{
			if(rs != null){
				rs.close();
			}
			if(pstmt != null){
				pstmt.close();
			}
		}
		return uid;
	}


}